package com.yitong.base;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


@Configuration  
public class DBConfig {
	
	@Autowired
    private Environment env;

	@Primary
    @Bean(name="firstDataSource")
	@ConfigurationProperties(prefix = "spring.first.datasource")
    public DataSource firstDataSource() {
		return DataSourceBuilder.create().build();//new org.apache.tomcat.jdbc.pool.DataSource();  
    }
	
	@Primary
	@Bean(name = "firstJdbcTemplate")
	public JdbcTemplate primaryJdbcTemplate() {
		return new JdbcTemplate(firstDataSource());
	}
	
	@Primary
	@Bean(name="firstTxManager")  
    public PlatformTransactionManager firstTransactionManager(@Qualifier("firstDataSource") DataSource dataSource) {  
        return new DataSourceTransactionManager(dataSource);  
    }
	
	
	
	
    @Bean(name="secondDataSource")
    @ConfigurationProperties(prefix = "spring.second.datasource")
    public DataSource secondDataSource() {
    	return DataSourceBuilder.create().build();//new org.apache.tomcat.jdbc.pool.DataSource();  
    }

    @Bean(name = "secondJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate() {
        return new JdbcTemplate(secondDataSource());
    }
    
	@Bean(name="secondTxManager")  
    public PlatformTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {  
        return new DataSourceTransactionManager(dataSource);  
    }
    
    
}
